import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store/index'
import Layout from '@/views/Layout/index.vue'

const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      { path: '/', component: () => import('@/views/Home/index.vue') },
      { path: '/category/:id', component: () => import('@/views/Category/index.vue') },
      { path: '/goods/:id', component: () => import('@/views/Goods/index.vue') }
    ]
  },
  { path: '/test', component: () => import('@/views/Test/index.vue') },
  { path: '/login', component: () => import('@/views/Login/index.vue') },
  { path: '/login/callback', component: () => import('@/views/Login/Callback.vue') }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
/**
 * to           去哪里
 * from         从哪里来
 * next         放行函数
 * 路由设计     需要用户登陆后才能访问的页面，统一都是 /member 开头
  */
router.beforeEach((to, from, next) => {
  if (to.path.startsWith('/member') && !store.state.user.profile.token) {
    next('/login?redirectUrl=' + to.fullPath)
  } else {
    next()
  }
})
export default router
